//
//  YGStatusRetweetView.swift
//  sinaWeiBoGui
//
//  Created by liyonggui on 16/5/18.
//  Copyright © 2016年 liyonggui. All rights reserved.
//

import UIKit

class YGStatusRetweetView: UIView {

    /// 被转发的模型属性
    var retweetStatus: YGStatus? {
        didSet {
            // 被转发微博的文本内容
            if retweetStatus == nil {
                retweetContentLabel.text = nil
            } else {
                let name = retweetStatus?.user?.screen_name ?? "未知用户名"
                let text = retweetStatus?.text ?? ""
                retweetContentLabel.text = "@\(name): \(text)"
            }
            
            // 被转发的图片
            retweetPictureView.pictureURLs = retweetStatus?.pictureURLs
            
            // 如果没有转发微博,重新更新retweetContentLabel.top的约束
            retweetContentLabel.snp_updateConstraints { (make) -> Void in
                let margin = retweetStatus == nil ? 0 : cellMargin
                make.top.equalTo(self).offset(margin)
            }
            
            // 重新更新配图的顶部
            retweetPictureView.snp_updateConstraints { (make) -> Void in
                var margin: CGFloat = cellMargin
                if retweetStatus == nil {
                    margin = 0
                } else if retweetStatus?.pictureURLs == nil || retweetStatus?.pictureURLs?.count == 0 {
                    margin = 0
                }
                make.top.equalTo(retweetContentLabel.snp_bottom).offset(margin)
            }
            
            // 被转发view的底部
            self.snp_updateConstraints { (make) -> Void in
                let margin = retweetStatus == nil ? 0 : cellMargin
                make.bottom.equalTo(retweetPictureView).offset(margin)
            }
        }
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        prepareUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func prepareUI() {
//        backgroundColor = UIColor.greenColor()
//        retweetPictureView.backgroundColor = UIColor.brownColor()
        // 添加控件
        addSubview(retweetContentLabel)
        addSubview(retweetPictureView)
        
        // 添加约束
        retweetContentLabel.snp_makeConstraints { (make) -> Void in
            make.leading.equalTo(self).offset(cellMargin)
            make.top.equalTo(self).offset(cellMargin)
        }
        
        // 被转发的配图
        retweetPictureView.snp_makeConstraints { (make) -> Void in
            make.leading.equalTo(retweetContentLabel)
            make.top.equalTo(retweetContentLabel.snp_bottom).offset(cellMargin)
            make.size.equalTo(CGSize(width: 200, height: 200))
        }
        
        // 自身的底部约束
        self.snp_makeConstraints { (make) -> Void in
            make.bottom.equalTo(retweetPictureView).offset(cellMargin)
        }
    }
    
    // MARK: - 懒加载
    private lazy var retweetContentLabel: UILabel = {
        let label = UILabel()
        
        // 设置
        label.textColor = UIColor.darkGrayColor()
        label.font = UIFont.systemFontOfSize(cellStatusFontSize)
        label.numberOfLines = 0
        
        label.text = "转发微博转发微博转发微博转发微博转发微博转发微博转发微博转发微博转发微博转发微博转发微博转发微博转发微博 "
        
        // 最大宽度
        label.preferredMaxLayoutWidth = UIScreen.mainScreen().bounds.width - 2 * cellMargin
        
        return label
    }()
    
    // 被转发的配图
    private lazy var retweetPictureView: YGStatusPictureView = YGStatusPictureView()

}
